package com.chuiyuan.utils;

import com.chuiyuan.entity.Rule;
import com.chuiyuan.exception.CommonException;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by chuiyuan on 2/11/16.
 */
public class HttpUtils {
    /**
     * get
     * @param rule
     * @return
     */
    private static String doGet(Rule rule){
        URL url ;
        String html ="";
        HttpURLConnection conn =null ;
        try {
            url = new URL(rule.getUrl());
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            if (conn.getResponseCode()==200){
                InputStream in = conn.getInputStream();
                html = StreamUtils.stream2String(in);
            }else {
                throw new CommonException("Http response code !=200");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            conn.disconnect(); //
        }
        return  html ;
    }

    /**
     *
     * @param rule
     * @return
     */
    private static String doPost (Rule rule){
        return  null ;
    }
}
